home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / renaisnc / rcnstrct.lha / Reconstruct / iris.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-04  |  8.0 KB  |  422 lines

  1. #include <gl.h>
  2. #include <device.h>
  3. #include <stdio.h>
  4.  
  5. #include "Reconstruct.h"
  6.  
  7. #ifndef NULL
  8. #define NULL 0L
  9. #endif
  10.  
  11. extern int Animating;
  12.  
  13. static long win;
  14. static int AnimationFrozen;
  15. static int Outline = FALSE, ShowPoint = FALSE;
  16.  
  17. static double xL, xH, yL, yH, xl, yl, xh, yh;
  18.  
  19. AnimateInitialize( XL,YL,XH,YH) 
  20.      double XL, XH, YL, YH;
  21. {
  22.   if (!Animating) return;
  23.  
  24.   xL = xl = XL; xH = xh = YH;
  25.   yL = yl = YL; yH = yh = YH;
  26.   ortho2( (Coord) xL, (Coord) xH, (Coord) yL, (Coord) yH );
  27. }
  28.  
  29.  
  30. AnimateShadeCell( Boundary, Sample )
  31.      RectType *Boundary;
  32.      SampleData *Sample;
  33. {
  34.   short r, g, b;
  35.  
  36.   if (!Animating || AnimationFrozen) return;
  37.  
  38.   r = Sample->Ave.r*256;
  39.   g = Sample->Ave.g*256;  
  40.   b = Sample->Ave.b*256;
  41.  
  42.   if (r > 255) r = 255;  if (r < 0) r = 0;
  43.   if (g > 255) g = 255;  if (g < 0) g = 0;
  44.   if (b > 255) b = 255;  if (b < 0) b = 0;
  45.  
  46.   bgnpolygon();
  47.   {
  48.     double p[2];
  49.  
  50.     RGBcolor( r, g, b );
  51.     p[0] = Boundary->xL;
  52.     p[1] = Boundary->yL;
  53.     v2d( p );
  54.  
  55.  
  56.     p[0] = Boundary->xL;
  57.     p[1] = Boundary->yH;
  58.     v2d( p );
  59.  
  60.     p[0] = Boundary->xH;
  61.     p[1] = Boundary->yH;
  62.     v2d( p );
  63.  
  64.     p[0] = Boundary->xH;
  65.     p[1] = Boundary->yL;
  66.     v2d( p );
  67.  
  68.   }
  69.   endpolygon();
  70.  
  71.  
  72.   if (Outline) {
  73.     bgnclosedline();
  74.     {
  75.       double p[2];
  76.  
  77.       RGBcolor( 255, 255, 255 );
  78.  
  79.       p[0] = Boundary->xL;
  80.       p[1] = Boundary->yL;
  81.       v2d( p );
  82.  
  83.       p[0] = Boundary->xL;
  84.       p[1] = Boundary->yH;
  85.       v2d( p );
  86.  
  87.       p[0] = Boundary->xH;
  88.       p[1] = Boundary->yH;
  89.       v2d( p );
  90.  
  91.       p[0] = Boundary->xH;
  92.       p[1] = Boundary->yL;
  93.       v2d( p );
  94.     }
  95.     endclosedline();
  96.   }
  97.  
  98.   if (ShowPoint) {
  99.     bgnpoint();
  100.     { 
  101.       double p[2];
  102.  
  103.       p[0] = Sample->Xs; p[1] = Sample->Ys;
  104.       RGBcolor( (short) 255, (short) 255, (short) 255 );
  105.       v2d( p );
  106.     }
  107.     endpoint();
  108.   }
  109. }
  110.  
  111.  
  112. void Animate (Animation)
  113.      char *Animation;
  114. {
  115.   Animating = TRUE;
  116.   
  117.   foreground();
  118.  
  119.   /* Set up default window size and options*/
  120.   prefsize( 768, 768 );
  121.   maxsize ( 1024, 1024 );
  122.   minsize (   32, 32 );
  123.   keepaspect( 1, 1 );
  124.  
  125.   /* Open the window */
  126.   win = winopen("Delaunay");
  127.  
  128.   /* Initialize the graphics state */
  129.   RGBmode();
  130.   singlebuffer(); 
  131.   gconfig();
  132.   ortho2(-1.0,1.0,-1.0,1.0);
  133.   clear();
  134.  
  135.   /* Set up devices to queue */
  136.   qdevice( REDRAW );
  137.   qdevice( WINQUIT );
  138.   qdevice( WINFREEZE );
  139.   qdevice( WINTHAW );
  140.  
  141.   qdevice( LEFTMOUSE );
  142.   qdevice( MIDDLEMOUSE );
  143.   tie( MIDDLEMOUSE, MOUSEX, MOUSEY );
  144.   qdevice( RIGHTMOUSE );
  145.  
  146.   qdevice( LEFTARROWKEY );
  147.   qdevice( RIGHTARROWKEY );
  148.   qdevice( DOWNARROWKEY );
  149.   qdevice( UPARROWKEY );
  150.  
  151.   qdevice( F1KEY );
  152.   qdevice( F2KEY );
  153.   qdevice( F3KEY );
  154.   qdevice( F4KEY );
  155.   qdevice( F5KEY );
  156.  
  157.   unqdevice( INPUTCHANGE );
  158.  
  159.  
  160. static void AnimateReDraw()
  161. {
  162.   extern HierarchicalRegion Root;
  163.   extern RectType RootBoundary;
  164.   register HierarchicalRegion *Node;
  165.   RectType LeftBoundary, RightBoundary, Boundary, Overlap;
  166.  
  167.   /* This stack is used to avoid the expense of recursion */
  168.   HierarchicalRegion *NodeStack[MAX_DEPTH];
  169.   RectType BoundaryStack[MAX_DEPTH];
  170.   int levelStack[MAX_DEPTH];
  171.   int nStack, level;
  172.   
  173.   if (AnimationFrozen) return;
  174.   
  175.   
  176.   /* Make sure the transformation is restored to the right state */
  177.   reshapeviewport();
  178.   
  179.  
  180.   /* Start with the root node on the stack */
  181.   nStack = 0;
  182.   NodeStack[nStack] = &Root;
  183.   BoundaryStack[nStack] = RootBoundary;
  184.   levelStack[nStack++] = 0;
  185.  
  186.   while (nStack > 0) {
  187.  
  188.     /* Get the next node to process from the stack */
  189.     Node = NodeStack[--nStack];
  190.     Boundary = BoundaryStack[nStack];
  191.     level    = levelStack[nStack];
  192.     
  193.     /* Follow the tree down a leaf, pushing the side branches.
  194.      */
  195.     while (! IsLeaf(Node)) {
  196.  
  197.       SplitBoundary( TRUE, Node->splitDirection, Node->splitValue,
  198.             &Boundary, &LeftBoundary );
  199.       if (Node->Left) {
  200.     NodeStack[nStack] = Node->Left;
  201.     BoundaryStack[nStack] = LeftBoundary;
  202.     levelStack[nStack++] = level+1;
  203.       }
  204.       SplitBoundary( FALSE, Node->splitDirection, Node->splitValue, 
  205.             &Boundary, &RightBoundary );
  206.       if (Node->Right) {
  207.     if (nStack >= MAX_DEPTH) {
  208.       fprintf( stderr, "Filter stack overflow!\n"  );
  209.       abort();
  210.     }
  211.     NodeStack[nStack] = Node->Right;
  212.     BoundaryStack[nStack] = RightBoundary;
  213.     levelStack[nStack++] = level+1;
  214.       }
  215.  
  216.       /* Get the next node to process from the stack */
  217.       Node = NodeStack[--nStack];
  218.       Boundary = BoundaryStack[nStack];
  219.       level    = levelStack[nStack];
  220.     }
  221.   
  222.     if (Node == NULL)
  223.       continue;        /* Dead end, go back to the stack loop */
  224.   
  225.     AnimateShadeCell( &Boundary, Node->Sample );
  226.   }
  227. }
  228.  
  229.  
  230.  
  231. AnimateDoEvent()
  232. {
  233.   Device dev;
  234.   short  data;
  235.   double range;
  236.   int update = FALSE;
  237.  
  238.   if (!Animating) return;
  239.  
  240.   while (qtest()) {
  241.     dev=qread(&data) ;
  242.     switch (dev) 
  243.       {
  244.       case REDRAW:
  245.     AnimateReDraw(); 
  246.     break;
  247.       case WINQUIT:
  248.     exit(0);
  249.       case WINFREEZE:
  250.     AnimationFrozen = TRUE;
  251.     break;
  252.       case WINTHAW:
  253.     AnimationFrozen = FALSE;
  254.     winset( win );
  255.     break;
  256.     
  257.       case F1KEY:
  258.     if (data != 1) break; /* Button down only */
  259.     /* Reset */
  260.     xL = xl; yL = yl; xH=xh; yH=yh;
  261.     update = TRUE;
  262.     break;
  263.     
  264.       case F2KEY:
  265.     if (data != 1) break; /* Button down only */
  266.     Outline = !Outline;
  267.     update = TRUE;;
  268.     break;
  269.     
  270.  
  271.       case F3KEY:
  272.     if (data != 1) break; /* Button down only */
  273.     ShowPoint = !ShowPoint;
  274.     update = TRUE;
  275.     break;
  276.  
  277.       case F4KEY:
  278.     if (data != 1) break;  /* Button down only */
  279.     fprintf( stderr, "%lf %lf %lf%lf\n", xL, yL, xH, yH );
  280.     break;
  281.  
  282.       case F5KEY:
  283.     if (data != 1) break;
  284.     fprintf( stderr, "Enter box xl,yl,xh,yh: " );
  285.     {
  286.       FILE *term = fopen( "/dev/tty", "r" );
  287.       int n = fscanf ( term, "%lf %lf %lf %lf", &xL, &yL, &xH, &yH );
  288.       fclose(term);
  289.     }
  290.     update = TRUE;
  291.     break;
  292.  
  293.       case LEFTMOUSE:
  294.     if (data != 1) break; /* Button down only */
  295.     /* Zoom out */
  296.     range = xH - xL;
  297.     range *= 0.05;
  298.     xL -= range;
  299.     xH += range;
  300.     
  301.     range = yH - yL;
  302.     range *= 0.05;
  303.     yL -= range;
  304.     yH += range;
  305.     update = TRUE;
  306.     AnimateReDraw();
  307.     break; 
  308.  
  309.       case MIDDLEMOUSE:
  310.     {
  311.       static double xl, yl, xh, yh;
  312.       int x_origin, y_origin, x_size, y_size;
  313.  
  314.       getorigin( &x_origin, &y_origin );
  315.       getsize  ( &x_size  , &y_size   );
  316.  
  317.       if (data == 1) {  /* Button Down */
  318.         /* Next two events ought to be mousex and mousey */
  319.         dev = qread( &data );
  320.         xl = (double) (data - x_origin) * (xH - xL) / (x_size) + xL;
  321.         dev = qread( &data );
  322.         yl = (double) (data - y_origin) * (yH - yL) / (y_size) + yL;
  323.       } else {  /* Mouse Up */
  324.         /* Next two events ought to be mousex and mousey */
  325.         dev = qread( &data );
  326.         xh = (double) (data - x_origin) * (xH - xL) / (x_size) + xL;
  327.         dev = qread( &data );
  328.         yh = (double) (data - y_origin) * (yH - yL) / (y_size) + yL;
  329.         if (xl > xh) { xL = xh; xH = xl;} else { xL = xl; xH = xh; }
  330.         if (yl > yh) { yL = yh; yH = yl;} else { yL = yl; yH = yh; }
  331.         update = TRUE;
  332.       }
  333.       break;
  334.     }
  335.  
  336.       case RIGHTMOUSE:
  337.     if (data != 1) break; /* Button down only */
  338.     /* Zoom in */
  339.     range = xH - xL;
  340.     range *= 0.05;
  341.     xL += range;
  342.     xH -= range;
  343.     
  344.     range = yH - yL;
  345.     range *= 0.05;
  346.     yL += range;
  347.     yH -= range;
  348.     update = TRUE;
  349.     break;
  350.     
  351.       case LEFTARROWKEY:
  352.     if (data != 1) break; /* Button down only */
  353.     /* Move left (negative) */
  354.     range = xH - xL;
  355.     range *= .05;
  356.     xL -= range;
  357.     xH -= range;
  358.     update = TRUE;
  359.     break;
  360.     
  361.       case RIGHTARROWKEY:
  362.     if (data != 1) break; /* Button down only */
  363.     /* Move right (positive) */
  364.     range = xH - xL;
  365.     range *= .05;
  366.     xL += range;
  367.     xH += range;
  368.     update = TRUE;
  369.     break;
  370.     
  371.       case DOWNARROWKEY:
  372.     if (data != 1) break; /* Button down only */
  373.     /* Move down (negative) */
  374.     range = yH - yL;
  375.     range *= .05;
  376.     yL -= range;
  377.     yH -= range;
  378.     update = TRUE;
  379.     break;
  380.     
  381.       case UPARROWKEY:
  382.     if (data != 1) break; /* Button down only */
  383.     /* Move up (positive) */
  384.     range = yH - yL;
  385.     range *= .05;
  386.     yL += range;
  387.     yH += range;
  388.     update = TRUE;
  389.     break;
  390.     
  391.       }
  392.   }
  393.   if (update) {
  394.     ortho2( (Coord) xL, (Coord) xH, (Coord) yL, (Coord) yH );
  395.     RGBcolor(0,0,0); clear();
  396.     AnimateReDraw();
  397.   }    
  398.   return dev;
  399. }
  400.  
  401. int AnimateCheckInput()
  402. {
  403.   if (!Animating) return;
  404.  
  405.   while ( qtest() )
  406.     (void) AnimateDoEvent();
  407. }
  408.  
  409.  
  410. AnimateExit()
  411. {
  412.  
  413.   if (!Animating) return;
  414.  
  415.   do {}  while (AnimateDoEvent() != WINQUIT);
  416. }
  417.   
  418.  
  419.  
  420.  
  421.